﻿namespace PowerSong.UserInterface
    {
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using PowerSong.SongDatabase;

    /// <summary>
    /// Interaction logic for EditStyleElementWindow.xaml
    /// </summary>
    public partial class EditStyleElementWindow : Window
        {

        private int projectorID = -1;

        private Theatre theatre = null;

        private PowerSong.SongDatabase.Style parentStyle = null;

        private SubStyle style = null;

        private Border resizer = null;

        public EditStyleElementWindow(Theatre theatre, PowerSong.SongDatabase.Style parentStyle, SubStyle style)
            {
            InitializeComponent();
            this.Font.SelectedName = style.FontName;
            this.Font.SelectedSize = style.FontSize;
            this.Font.SelectedStyle = style.FontStyle;

            this.parentStyle = parentStyle;
            this.style = style;
            this.theatre = theatre;
            this.projectorID = this.theatre.CreateProjector(this.PreviewGrid);
            this.Loaded += EditStyleElementWindow_Loaded;
            this.Closed += EditStyleElementWindow_Closed;
            }

        private void EditStyleElementWindow_Loaded(object sender, RoutedEventArgs e)
            {

            // Create items in combo boxes
            this.AlignmentComboBox.ItemsSource = Utilities.CreateList<Alignment>();
            this.FadeInStyleComboBox.ItemsSource = Utilities.CreateList<FadeType>();
            this.FadeOutStyleComboBox.ItemsSource = Utilities.CreateList<FadeType>();
            this.CrossFadeStyleComboBox.ItemsSource = Utilities.CreateList<FadeType>();
            this.EffectComboBox.ItemsSource = Utilities.CreateList<TextEffectType>();
            this.VisibilityComboBox.ItemsSource = Utilities.CreateList<TextVisibility>();

            // Populate fade types
            this.FadeInStyleComboBox.SelectedValue = style.FadeIn.Type;
            this.FadeOutStyleComboBox.SelectedValue = style.FadeOut.Type;
            this.CrossFadeStyleComboBox.SelectedValue = style.CrossFade.Type;

            // Populate durations
            this.FadeInDurationTextBox.Text = style.FadeIn.Duration.ToString();
            this.FadeOutDurationTextBox.Text = style.FadeOut.Duration.ToString();
            this.CrossFadeDurationTextBox.Text = style.CrossFade.Duration.ToString();

            // Populate other things
            this.AlignmentComboBox.SelectedValue = style.TextAlignment;
            this.VisibilityComboBox.SelectedValue = style.Visibility;
            this.EffectComboBox.SelectedValue = style.FontEffect;
            this.MarginLeftTextBox.Text = style.Margin.Left.ToString();
            this.MarginTopTextBox.Text = style.Margin.Top.ToString();
            this.MarginRightTextBox.Text = style.Margin.Right.ToString();
            this.MarginBottomTextBox.Text = style.Margin.Bottom.ToString();

            // Show resizer control
            this.resizer = new Border() { BorderThickness = new Thickness(3), BorderBrush = Brushes.Tomato };
            this.PreviewGrid.Children.Add(resizer);
            this.resizer.Margin = new Thickness(style.Margin.Left, style.Margin.Top, style.Margin.Right, style.Margin.Bottom);

            }

        private void EditStyleElementWindow_Closed(object sender, EventArgs e)
            {
            this.theatre.RemoveProjector(projectorID);
            }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
            {
            this.DialogResult = false;
            }

        private void OKButton_Click(object sender, RoutedEventArgs e)
            {

            // Update the style details
            this.style.FadeIn.Type = (FadeType)FadeInStyleComboBox.SelectedValue;
            this.style.FadeOut.Type = (FadeType)FadeOutStyleComboBox.SelectedValue;
            this.style.CrossFade.Type = (FadeType)CrossFadeStyleComboBox.SelectedValue;
            this.style.FadeIn.Duration = int.Parse(FadeInDurationTextBox.Text);
            this.style.FadeOut.Duration = int.Parse(FadeOutDurationTextBox.Text);
            this.style.CrossFade.Duration = int.Parse(CrossFadeDurationTextBox.Text);
            this.style.TextAlignment = (Alignment)AlignmentComboBox.SelectedValue;
            this.style.Visibility = (TextVisibility)VisibilityComboBox.SelectedValue;
            this.style.FontEffect = (TextEffectType)EffectComboBox.SelectedValue;
            this.style.Margin.Left = double.Parse(MarginLeftTextBox.Text);
            this.style.Margin.Top = double.Parse(MarginTopTextBox.Text);
            this.style.Margin.Right = double.Parse(MarginRightTextBox.Text);
            this.style.Margin.Bottom = double.Parse(MarginBottomTextBox.Text);
            State.Database.Styles.Write(this.parentStyle);

            // Close the window
            this.DialogResult = true;

            }

        private void MarginTextBoxChanged(object sender, TextChangedEventArgs e)
            {
            if (resizer != null)
                {
                var left = 0D;
                var top = 0D;
                var right = 0D;
                var bottom = 0D;
                double.TryParse(MarginLeftTextBox.Text, out left);
                double.TryParse(MarginTopTextBox.Text, out top);
                double.TryParse(MarginRightTextBox.Text, out right);
                double.TryParse(MarginBottomTextBox.Text, out bottom);
                resizer.Margin = new Thickness(left * PreviewGrid.ActualWidth,
                                               top * PreviewGrid.ActualHeight,
                                               PreviewGrid.ActualWidth - right * PreviewGrid.ActualWidth,
                                               PreviewGrid.ActualHeight - bottom * PreviewGrid.ActualHeight);
                }
            }

        }

    }
